package cn.rui.service.impl;

import cn.rui.entity.PageResult;
import cn.rui.entity.QueryPageBean;
import cn.rui.mapper.PermissionMapper;
import cn.rui.pojo.Permission;
import cn.rui.service.PermissionService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 徽州大都督
 * @date 2020/9/3
 */
@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * 分页查询权限
     *
     * @param
     * @return
     */
    public List<Permission> findPageByRoleId() {

        List<Permission> permissionList = permissionMapper.findPageByRoleId ();

        return permissionList;
    }

    /**
     * 带搜索条件分页查询
     */
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage ();
        Integer pageSize = queryPageBean.getPageSize ();
        String queryString = queryPageBean.getQueryString ();
        PageHelper.startPage (currentPage,pageSize);
        Page<Permission> page=permissionMapper.findPage(queryString);
        long total = page.getTotal ();
        List<Permission> result = page.getResult ();
        return new PageResult (total,result);
    }

    /**
     * 添加权限
     * @param permission
     */
    public void handleAdd(Permission permission) {
        permission.setDate (new Date ());
        permissionMapper.add(permission);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public Permission findById(Integer id) {
        return permissionMapper.findById(id);
    }

    /**
     * 编辑
     * @param permission
     */
    public void handleEdit(Permission permission) {
        permissionMapper.handleEdit(permission);
    }

    /**
     * 根据id删除
     * @param id
     */
    public void handleDelete(Integer id) {

        //删除关联的role
        permissionMapper.deleteRoleAndPermissionById(id);
        permissionMapper.deleteById(id);
    }
}
